package com.sn.seabattle;

import java.io.Serializable;

public class Cell implements Serializable {
    private int value;
    private Ship ship = null;
    private boolean hidden = false;

    public Cell(int v) {
        this.value = v;
    }

    public void setValue(int v) {
        if (v >= 0 && v <= 4) {
            value = v;
        }
    }

    public void hide() {
        this.hidden = true;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setHide(boolean o) {
        this.hidden = o;
    }

    public int attack(Cell[][] area) {
        this.hidden = false;
        if (value == 0) {
            value = 1;
            return 0;
        }
        if (value == 2) {
            value = 3;
            return ship.attack(area);
        }
        return -1;
    }

    public int getValueTrue() {
        return value;
    }

    public int getValue() {
        if (hidden) {
            return 0;
        } else {
            return value;
        }
    }

    public Ship getShip() {
        return ship;
    }

    public void setShip(Ship s) {
        this.ship = s;
    }

    public int getX(Cell[][] area) {
        for (int i = 0; i < area.length; i++) {
            for (int j = 0; j < area[0].length; j++) {
                if (area[i][j] == this) {
                    return j;
                }
            }
        }
        return 0;
    }

    public int getY(Cell[][] area) {
        for (int i = 0; i < area.length; i++) {
            for (int j = 0; j < area[0].length; j++) {
                if (area[i][j] == this) {
                    return i;
                }
            }
        }
        return 0;
    }

    public int NearShips(Cell[][] area) {
        int i = getY(area);
        int j = getX(area);
        int ships = 0;
        if (i > 0) {
            if (area[i - 1][j].getValue() == 2) {
                ships++;
            }
            if (j > 0) {
                if (area[i - 1][j - 1].getValue() == 2) {
                    ships++;
                }
            }
            if (j < area[0].length - 1) {
                if (area[i - 1][j + 1].getValue() == 2) {
                    ships++;
                }
            }
        }
        if (j > 0) {
            if (area[i][j - 1].getValue() == 2) {
                ships++;
            }
        }
        if (j < area[0].length - 1) {
            if (area[i][j + 1].getValue() == 2) {
                ships++;
            }
        }
        if (i < area.length - 1) {
            if (area[i + 1][j].getValue() == 2) {
                ships++;
            }
            if (j > 0) {
                if (area[i + 1][j - 1].getValue() == 2) {
                    ships++;
                }
            }
            if (j < area[0].length - 1) {
                if (area[i + 1][j + 1].getValue() == 2) {
                    ships++;
                }
            }
        }
        return ships;
    }

    public void border(Cell[][] area) {
        int i = getY(area);
        int j = getX(area);
        if (i > 0) {
            if (area[i - 1][j].getValue() == 0) {
                area[i - 1][j].setValue(1);
            }
            if (j > 0) {
                if (area[i - 1][j - 1].getValue() == 0) {
                    area[i - 1][j - 1].setValue(1);
                }
            }
            if (j < area[0].length - 1) {
                if (area[i - 1][j + 1].getValue() == 0) {
                    area[i - 1][j + 1].setValue(1);
                }
            }
        }
        if (j > 0) {
            if (area[i][j - 1].getValue() == 0) {
                area[i][j - 1].setValue(1);
            }
        }
        if (j < area[0].length - 1) {
            if (area[i][j + 1].getValue() == 0) {
                area[i][j + 1].setValue(1);
            }
        }
        if (i < area.length - 1) {
            if (area[i + 1][j].getValue() == 0) {
                area[i + 1][j].setValue(1);
            }
            if (j > 0) {
                if (area[i + 1][j - 1].getValue() == 0) {
                    area[i + 1][j - 1].setValue(1);
                }
            }
            if (j < area[0].length - 1) {
                if (area[i + 1][j + 1].getValue() == 0) {
                    area[i + 1][j + 1].setValue(1);
                }
            }
        }
    }

    public void deborder(Cell[][] area) {
        int i = getY(area);
        int j = getX(area);
        if (i > 0) {
            if (area[i - 1][j].getValue() == 1
                    && area[i - 1][j].NearShips(area) == 1) {
                area[i - 1][j].setValue(0);
            }
            if (j > 0) {
                if (area[i - 1][j - 1].getValue() == 1
                        && area[i - 1][j - 1].NearShips(area) == 1) {
                    area[i - 1][j - 1].setValue(0);
                }
            }
            if (j < area[0].length - 1) {
                if (area[i - 1][j + 1].getValue() == 1
                        && area[i - 1][j + 1].NearShips(area) == 1) {
                    area[i - 1][j + 1].setValue(0);
                }
            }
        }
        if (j > 0) {
            if (area[i][j - 1].getValue() == 1
                    && area[i][j - 1].NearShips(area) == 1) {
                area[i][j - 1].setValue(0);
            }
        }
        if (j < area[0].length - 1) {
            if (area[i][j + 1].getValue() == 1
                    && area[i][j + 1].NearShips(area) == 1) {
                area[i][j + 1].setValue(0);
            }
        }
        if (i < area.length - 1) {
            if (area[i + 1][j].getValue() == 1
                    && area[i + 1][j].NearShips(area) == 1) {
                area[i + 1][j].setValue(0);
            }
            if (j > 0) {
                if (area[i + 1][j - 1].getValue() == 1
                        && area[i + 1][j - 1].NearShips(area) == 1) {
                    area[i + 1][j - 1].setValue(0);
                }
            }
            if (j < area[0].length - 1) {
                if (area[i + 1][j + 1].getValue() == 1
                        && area[i + 1][j + 1].NearShips(area) == 1) {
                    area[i + 1][j + 1].setValue(0);
                }
            }
        }
    }

    public void borderH(Cell[][] area) {
        int i = getY(area);
        int j = getX(area);
        if (i > 0) {
            if (area[i - 1][j].getValue() == 0) {
                area[i - 1][j].setValue(1);
                area[i - 1][j].setHide(false);
            }

            if (j > 0) {
                if (area[i - 1][j - 1].getValue() == 0) {
                    area[i - 1][j - 1].setValue(1);
                    area[i - 1][j - 1].setHide(false);
                }

            }
            if (j < area[0].length - 1) {
                if (area[i - 1][j + 1].getValue() == 0) {
                    area[i - 1][j + 1].setValue(1);
                    area[i - 1][j + 1].setHide(false);
                }

            }
        }
        if (j > 0) {
            if (area[i][j - 1].getValue() == 0) {
                area[i][j - 1].setValue(1);
                area[i][j - 1].setHide(false);
            }

        }
        if (j < area[0].length - 1) {
            if (area[i][j + 1].getValue() == 0) {
                area[i][j + 1].setValue(1);
                area[i][j + 1].setHide(false);
            }

        }
        if (i < area.length - 1) {
            if (area[i + 1][j].getValue() == 0) {
                area[i + 1][j].setValue(1);
                area[i + 1][j].setHide(false);
            }

            if (j > 0) {
                if (area[i + 1][j - 1].getValue() == 0) {
                    area[i + 1][j - 1].setValue(1);
                    area[i + 1][j - 1].setHide(false);
                }

            }
            if (j < area[0].length - 1) {
                if (area[i + 1][j + 1].getValue() == 0) {
                    area[i + 1][j + 1].setValue(1);
                    area[i + 1][j + 1].setHide(false);
                }

            }
        }
    }
}
